package com.pengheng.transformation.base;

import com.pengheng.bean.WaterSensor;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * 过滤 基本转换算子
 */
public class TransFlatMap {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<WaterSensor> streamSource = env.fromElements(
                new WaterSensor("sensor1", 1L, 1),
                new WaterSensor("sensor1", 2L, 2),
                new WaterSensor("sensor2", 2L, 2),
                new WaterSensor("sensor3", 3L, 3)
        );

        streamSource.flatMap(new InnerFlatMap()).print();

        env.execute();


    }

    private static class InnerFlatMap implements FlatMapFunction<WaterSensor, String> {
        @Override
        public void flatMap(WaterSensor waterSensor, Collector<String> collector) throws Exception {
            if (waterSensor.ts == 1L || waterSensor.vc == 2) {
                collector.collect(waterSensor.id + "-" + waterSensor.ts);
            }
        }
    }
}
